package stardust.product.spider.http;

import java.util.List;
import java.util.Map;
import stardust.core.support.db.DataAccessObject;
import stardust.core.support.db.StardustDB;
import stardust.product.spider.URLSource.URLEntity;

public class Main
{

  public static void main(String[] args)
  {
    
    DataAccessObject dao = StardustDB.getDataAccessObject();
    dao.execute("UPDATE TB_MOD_SPD_PAGE SET SP_FLAG = 0 WHERE SP_FLAG IN (1, 3)");
    dao.close(false);
    
    final SimpleURLSource urlSource = new SimpleURLSource
    (
      new SimpleURLSource.SimpleURLSourceFetcher()
      {
        
        public void fetch(SimpleURLSource urlSource)
        {
          DataAccessObject dao = StardustDB.getDataAccessObject();
          List<Map<String, Object>> list;
          
          try
          {
            list = dao.query("SELECT SP_URL, SP_REFERER FROM TB_MOD_SPD_PAGE WHERE SP_HOST IN ('db.gamersky.com', 'www.gamersky.com') AND (SP_FLAG = 0 OR SP_FLAG IS NULL) ORDER BY SP_TIME LIMIT 0, 100");
            
            for (int i = 0; i < list.size(); i++)
            {
              if (urlSource.append(new URLEntity((String) list.get(i).get("SP_URL"), (String) list.get(i).get("SP_REFERER"))))
              {
                dao.execute("UPDATE TB_MOD_SPD_PAGE SET SP_FLAG = 1 WHERE SP_URL = ?", list.get(i).get("SP_URL"));
                dao.execute("COMMIT");
              }
              else
              {
                break;
              }
            }
          }
          finally
          {
            dao.close(true);
          }
        }
      }
    );
    
    HTTPWebcSpider sp = new HTTPWebcSpider();
    
    sp.startup(urlSource, 8);
  }
  
}
